//
//  NSURLConnection
//  Internet
//  NSURLConnection GET
//

#import "ZYXNSURLConnectionViewController.h"
#import "MBProgressHUD+MJ.h"

@interface ZYXNSURLConnectionViewController ()
@property (weak, nonatomic) IBOutlet UITextField *username;
@property (weak, nonatomic) IBOutlet UITextField *pwd;
- (IBAction)login;

@end

@implementation ZYXNSURLConnectionViewController

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //退出键盘
    [self.view endEditing:YES];
}

- (IBAction)login
{
    //[self sendSynchronousRequest]; //实际开发中不使用同步HTTP请求
    
    
    [self sendAsynchronousRequest];  //异步HTTP请求
    
}

/**
 *  发送异步HTTP请求
 */
-(void)sendAsynchronousRequest
{
    // 1.表单验证(输入验证)
    
    //用户名
    if(self.username.text.length == 0){
        [MBProgressHUD showError:@"请输入用户名"];
        return;
    }
    
    //密码
    if(self.pwd.text.length == 0){
        [MBProgressHUD showError:@"请输入密码"];
        return;
    }
    
    // 弹框
    [MBProgressHUD showMessage:@"正在拼命加载中..."];
    
    
    // 2.发送请求给服务器(带上帐号和密码) | GET 请求
    
    //创建URl对象 请求路径
    NSString *urlStr = [NSString stringWithFormat:@"http://localhost:8080/MJServer/login?username=%@&pwd=%@",self.username.text,self.pwd.text];
    NSURL *url = [NSURL URLWithString:urlStr];
    
    //创建NSURLRequest对象
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    //发送HTTP请求
    
    //发送异步请求
    NSLog(@"begin----------------");
    
    //NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    //HTTP请求完成处理HTTP响应的block执行的线程是 <NSThread: 0x8e535c0>{name = (null), num = 3}
    
    NSOperationQueue *queue = [NSOperationQueue mainQueue];
    //HTTP请求完成处理HTTP响应的block执行的线程是 <NSThread: 0x8c0a0c0>{name = (null), num = 1}
    
    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response,NSData *data,NSError *connectionError){ // 当请求结束的时候调用 (拿到了服务器的数据, 请求失败)
        
        // 隐藏HUD (刷新UI界面, 一定要放在主线程, 不能放在子线程)
        [MBProgressHUD hideHUD];
        
        NSLog(@"HTTP请求完成处理HTTP响应的block执行的线程是 %@",[NSThread currentThread]);
        
        //该block代码会在该HTTP请求 得到服务器响应后自动调用
        if(connectionError || data==nil){
            [MBProgressHUD showError:@"请求失败"];
            return;
        }
        
        /**
         解析data :
         {"error":"用户名不存在"}
         {"error":"密码不正确"}
         {"success":"登录成功"}
         */
        
        //解析服务器返回的json数据
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        
        NSString *error = dict[@"error"];
        if(error){
            [MBProgressHUD showError:error];
        }else{
            [MBProgressHUD showSuccess:dict[@"success"]];
        }
    }];
    
    
    NSLog(@"end----------------");
    

}

/**
 *  在UI主线程发送同步方式的HTTP请求 请求期间阻塞主线程
 *  如果网速慢 服务器端的业务逻辑复杂 服务器很长时间才能返回数据给iOS端 UI主线程卡住 影响用户体验
 */
-(void)sendSynchronousRequest
{
    //用户名
    if(self.username.text.length == 0){
        [MBProgressHUD showError:@"请输入用户名"];
        return;
    }
    
    //密码
    if(self.pwd.text.length == 0){
        [MBProgressHUD showError:@"请输入密码"];
        return;
    }
    
    //http://localhost:8080/MJServer/login?username=789&pwd=111&method=get&type=JSON
    
    //创建URl对象 请求路径
    NSString *urlStr = [NSString stringWithFormat:@"http://localhost:8080/MJServer/login?username=%@&pwd=%@",self.username.text,self.pwd.text];
    NSURL *url = [NSURL URLWithString:urlStr];
    
    //创建NSURLRequest对象
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    //发送HTTP请求
    
    //发送同步请求
    NSLog(@"begin----------------");
    
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    
    //解析服务器返回的json数据
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
    
    NSString *error = dict[@"error"];
    if(error){
        [MBProgressHUD showError:error];
    }else{
        [MBProgressHUD showSuccess:dict[@"success"]];
    }
    
    NSLog(@"end----------------");
    NSLog(@"%@",data);
}






@end

























